/**
* -------------------------------------------------------------
* @author  cl
* @date: 2021-12-28 17:28:05
* @description: 上传文件bind表 serviceImpl
* -------------------------------------------------------------
* @version 1.0.0
* @Copyright (c) 中科星图-版权所有
*/

package com.geovis.boot.service.demo.file.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.boot.service.demo.file.entity.FileBind;
import com.geovis.boot.service.demo.file.mapper.FileBindMapper;
import com.geovis.boot.service.demo.file.service.IFileBindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * 上传文件bind表接口实现
 * @author cl
 */
@Slf4j
@Service
@Transactional
public class IFileBindServiceImpl extends ServiceImpl<FileBindMapper, FileBind> implements IFileBindService {

    @Resource
    private FileBindMapper fileBindMapper;

    @Value("${file.upload_path}")
    private String uploadPath;

    @Value("${file.template_path}")
    private String templatePath;

    @Autowired
    private ResourceLoader resourceLoader;

    @Override
    public FileBind getById(String id) {

       return fileBindMapper.selectById(id);
    }

    @Override
    public List<FileBind> getList(String updateBy) {
        QueryWrapper<FileBind> wrapper = new QueryWrapper<>();
        wrapper.select("updateBy", updateBy);
        List<FileBind> fileBinds = fileBindMapper.selectList(wrapper);
        return fileBinds;
    }

    @Override
    public IPage getByParam(Long pageNum, Long pageSize, String uri, String path) {
        QueryWrapper<FileBind> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(uri) ){
            wrapper.eq("uri",uri);
        }
        if(!StringUtils.isEmpty(path) ){
            wrapper.eq("path",path);
        }
        Page<FileBind> page = new Page<>(pageNum,pageSize);
        IPage<FileBind> iPage = fileBindMapper.selectPage(page, wrapper);
        return iPage;
    }

    @Override
    public boolean saveOrUpdate(FileBind fileBind) {
        return super.saveOrUpdate(fileBind);
    }

    @Override
    public boolean removeById(String id) {
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean batchRemoveByIds(String[] ids) {
        return fileBindMapper.batchRemoveByIds(ids);
    }

    @Override
    public FileBind selectFileBinds(String id) {
        return fileBindMapper.selectFileBinds(id);
    }

    /**
     * 上传文件示例
     * @param files
     * @return
     */
    @Override
    public List<String> upload(MultipartFile files[]) {
        List<String> fileNameList=new LinkedList<>();
        for (MultipartFile file : files) {
            try {
                fileNameList.add(upload(file));
            }catch (Exception e){
                fileNameList.add(null);
            }
        }
        return fileNameList;
    }

    private String upload(MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename().replace(" ", "");
        String uuidName=System.currentTimeMillis()+"_"+ UUID.randomUUID().toString();
        uuidName=uuidName.replace("-","");
        File folder = new File(uploadPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String filePathName = uploadPath + uuidName;
        File file = new File(filePathName);
        try {
            org.apache.commons.io.FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
            return uuidName;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传文件失败，originalFilename="+originalFilename);
        }
    }

    @Override
    public org.springframework.core.io.Resource getTemplateResource(){
        return this.resourceLoader.getResource(templatePath);
    }

}
